package xyz.bali16.module.netdisk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.netdisk.entity.FileStorage;
import xyz.bali16.module.netdisk.model.filestorage.FileStorageAddParam;
import xyz.bali16.module.netdisk.model.filestorage.FileStoragePageParam;
import xyz.bali16.module.netdisk.model.filestorage.FileStorageUpdateParam;


import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static xyz.bali16.application.core.model.Result.*;


@RestController
@RequestMapping(BaseController.API+"filestorage")
public class FileStorageController extends NetdiskBaseController {
    @GetMapping
    @ApiOperation("文件仓库列表数据")
    //@PreAuthorize("@auth.permission('plugin:netdisk:filestorage:query')")
    public Result<IPage<FileStorage>> list(FileStoragePageParam param) {
        IPage<FileStorage> fileStorageIPage = fileStorageService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, fileStorageIPage);
    }

    @PostMapping
    @ApiOperation("添加文件仓库")
    //@PreAuthorize("@auth.permission('plugin:netdisk:filestorage:add')")
    public Result<String> add(@RequestBody @Valid FileStorageAddParam param) {
        try {
            fileStorageService.add(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.ADD_ERROR, "新增标签失败", e);
        }
    }

    @PutMapping
    //@PreAuthorize("@auth.permission('plugin:netdisk:filestorage:update')")
    public Result<String>  update(@RequestBody @Valid FileStorageUpdateParam param) {
        try {
            fileStorageService.update(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.UPDATE_ERROR, "更新标签失败", e);
        }
    }

    @DeleteMapping("/{fileStorageId}")
    @ApiOperation("删除文件仓库")
    @ApiImplicitParam(name = "fileStorageId", value = "文件仓库id", paramType = "path", required = true)
    //@PreAuthorize("@auth.permission('plugin:netdisk:filestorage:delete')")
    public Result<String> delete(@PathVariable("fileStorageId") String fileStorageId) {
        try {
            fileStorageService.delete(fileStorageId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }
}
